package com.longge.wechat.controller.wx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.longge.wechat.communication.Poster;
import com.longge.wechat.message.req.ReqLocationMessage;
import com.longge.wechat.message.resp.RespTextMessage;
import com.longge.wechat.message.wxUtil.WeiXinUtil;
import com.longge.wechat.utils.XmlUtil;


@Controller
public class PostLocationController {

	@RequestMapping(value = "/showLocation.do", method = RequestMethod.POST)
	protected void doPost1(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String content = "";
		String openId = (String) request.getAttribute("openId");
		
		content = "去吧";
	
		
		RespTextMessage message = new RespTextMessage(
				openId,
				(String) request.getAttribute("from"),content);
		Poster.post(response, XmlUtil.getInstance().bean2xml(message,null,null));
	}
	
	@RequestMapping(value = "/getLabel.do", method = RequestMethod.POST)
	public void getLabel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
		String xmlText = (String) request.getAttribute("xmlText");
		XmlUtil xmlUtil = XmlUtil.getInstance();
		ReqLocationMessage rlm = (ReqLocationMessage)xmlUtil.xml2bean(xmlText, ReqLocationMessage.class);
		
		
		String label = WeiXinUtil.getLabel(rlm.getLocation_X(), rlm.getLocation_Y());
		RespTextMessage message = new RespTextMessage(openId,from,label);
		Poster.post(response, XmlUtil.getInstance().bean2xml(message,null,null));
	    /*String ak = ReadProperties.loadProperties().getProperty("AK");
    	
    	 根据地理x，y坐标获取位置信息 
        String locationUrl = "http://api.map.baidu.com/geocoder/v2/?"
        		+ "ak="+ak+"&callback=renderReverse&location="+rlm.getLocation_X()+","+rlm.getLocation_Y()+"&output=xml&pois=0";
        
        xmlUtil.setURL(locationUrl);
        
        从url返回的xml文档中位置信息
        String str = xmlUtil.getContentByNodeName("formatted_address");
        System.out.println(str);*/
     
	}
}
